package summer.graph;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution1306 {
    public boolean canReach(int[] arr, int start) {
        boolean[] used = new boolean[arr.length];
        return dfs(used, arr, start);
    }

    public boolean dfs(boolean[] used, int[] arr, int index) {
        if (arr[index] == 0) return true;
        int pre = index - arr[index];
        if (pre >= 0 && !used[pre]) {
            used[pre] = true;
            if (dfs(used, arr, pre)) return true;
        }
        int post = index + arr[index];
        if (post < arr.length && !used[post]) {
            used[post] = true;
            if (dfs(used, arr, post)) return true;
        }
        return false;
    }
}
